package com.lkl.demo.consumer.config;

import com.lkl.demo.common.constant.RabbitMQConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicConfig {

    // 创建topic模式交换机，durable：是否持久化到erlang自带的数据库，autoDelete：当消息接收者宕机后，消息队列中的消息是否自动删除
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitMQConstant.EXCHANGE_NAME, true, false);
    }

    // 创建两个队列，一个处理短信，一个处理邮箱
    @Bean
    public Queue topicSMSQueue() {
        return new Queue(RabbitMQConstant.TOPIC_MESSAGE_SMS, true);
    }

    @Bean
    public Queue topicEmailQueue() {
        return new Queue(RabbitMQConstant.TOPIC_MESSAGE_EMAIL, true);
    }

    // 创建绑定关系 （*号表示必须要有一个相匹配，#表示随便0-n都可以相匹配）
    @Bean
    public Binding topicSMSBinding(){
        return BindingBuilder.bind(topicSMSQueue()).to(topicExchange()).with(RabbitMQConstant.ROUTER_KEY_SMS);
    }

    @Bean
    public Binding topicEmailBinding(){
        return BindingBuilder.bind(topicEmailQueue()).to(topicExchange()).with(RabbitMQConstant.ROUTER_KEY_EMAIL);
    }
}
